/**
 * @Author: LiMeng
 * @Date: 2017/6/5
 */
var getSelectedAddUser = function (data) {
    var names = '';
    var ids = '';
    for (var index in data) {
        names += ',' + data[index].realName;
        ids += ',' + data[index].id;
    }

    $('#addUserName').textbox('setValue', names.substring(1));
    $('#addUserId').val(ids.substring(1));
};
var getSelectedApplyUser = function (data) {
    var names = '';
    var ids = '';
    for (var index in data) {
        names += ',' + data[index].realName;
        ids += ',' + data[index].id;
    }

    $('#applyUserName').textbox('setValue', names.substring(1));
    $('#applyUserId').val(ids.substring(1));
};
var openSelectUser = function (title, callback) {
    WindowManager.openLayer({
        type: 2,
        title: title,
        area: ['900px', '500px'],
        content: ROOT_PATH + '/permission/user/simpleList.shtml',
        callBack: callback
    });
};
$(function () {
    $('#workTypeId').combobox({
        url: ROOT_PATH + '/permission/dictionary/queryDictionaryByDictionaryTypeCode.shtml?code=' + CONSTANT.DictionaryTypeDefConstants.JOBTYPE,
        valueField: 'id',
        editable: false
    });

    $('#workLevelId').combobox({
        url: ROOT_PATH + '/permission/dictionary/queryDictionaryByDictionaryTypeCode.shtml?code=' + CONSTANT.DictionaryTypeDefConstants.WORK_LEVEL,
        valueField: 'id',
        editable: false
    });

    $('#applyDeptId').combotree({
        url: ROOT_PATH + '/permission/department/getDepartmentTree.shtml?parentId=0',
        editable: false
    });

    $('#applyUserName').textbox({
        icons: [{
            iconCls: 'icon-search',
            handler: function () {
                openSelectUser('选择申请人', getSelectedApplyUser);
            }
        }],
        editable: false
    });

    $('#addUserName').textbox({
        icons: [{
            iconCls: 'icon-search',
            handler: function () {
                openSelectUser('选择登记人', getSelectedAddUser);
            }
        }],
        editable: false
    });

    $('#addDeptId').combotree({
        url: ROOT_PATH + '/permission/department/getDepartmentTree.shtml?parentId=0',
        editable: false
    });
    //初始化加载datagrid
    $('#dg1').datagrid({
        method: 'post',
        url: ROOT_PATH + '/workLicense/apply/list.shtml',
        queryParams: {
            workTypeId: $.getQueryParam('workTypeId'),
            applyId: $.getQueryParam('applyId'),
            filter: 1,
            sort: 'addTime',
            order: 'desc'
        },
        toolbar: '#toolbar',
        fitColumns: true,
        pagination: true,
        rownumbers: true,
        sortOrder: true,
        striped: true,
        singleSelect: true,
        fit: true,
        columns: [[
            {field: 'ckid', checkbox: true},
            {field: 'workTypeName', title: '作业类型', width: 60},
            {field: 'workLevelName', title: '作业级别', width: 60},
            {field: 'applyDeptName', title: '申请部门', width: 120},
            {field: 'workPoint', title: '作业地点', width: 120},
            {field: 'workContent', title: '作业内容', width: 120},
            {field: 'planStartDate', title: '计划开始日期', width: 100},
            {field: 'planEndDate', title: '计划结束日期', width: 100},
            {
                field: 'deptName', title: '施工单位', width: 120, formatter: function (value, row) {
                if (row.isCheckContractor === 1) {
                    return row.contractorAudit.contractorName + '<span style="color:red;"> [承包商]</span>';
                } else {
                    return value;
                }
            }
            },
            {
                field: 'auditState', title: '状态', width: 60, formatter: function (value, row) {
                var str = '';
                switch (value) {
                    case 0:
                        str = '已登记';
                        break;
                    case 1:
                        str = '审批中';
                        break;
                    case 7:
                        str = '已审批';
                        break;
                    case 8:
                        str = '已驳回';
                        break;
                }
                if (value === 0) {
                    return str;
                } else {
                    return "<a onclick=\"flowMonitorByBusinessId('" + row.id + "')\" style='color:blue;'>" + str + "</a>";
                }
            }
            }
        ]]
    });
    $('#btn_clear').on("click", function () {
        $('#tb1').form('reset');
        $('#tb2').form('reset').find(':hidden').val('');
    });
    $('#btn_search').on("click", function () {
        $('#dg1').datagrid('reload', $('#tb1,#tb2').find(':input').serializeJson());
    });
    $('#btn_advanced_search').on("click", function () {
        $('#tb2').toggle();
        $('#dg1').datagrid('resize');
    });
    $('#btn_save').on('click', function () {
        var $selected = $('#dg1').datagrid('getSelections');
        if ($selected.length === 0) {
            layer.alert("请选择一条数据！");
            return;
        }
        WindowManager.closeLayer({
            returnValue: $selected
        });
    });
});